package com.shenhonghui.twins.utils;

import com.shenhonghui.twins.ResourceTable;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.audio.AudioManager;
import ohos.media.audio.SoundPlayer;

import java.util.HashMap;

public class PianoMusic {
    HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,0x00205,"Music Player");
    //由于资源包含了钢琴的全部音效，特此备注：资源里与琴键的对应为：
    private int Music[] = {
            //大字组
            ResourceTable.Media_p16, //C
            ResourceTable.Media_p18, //D
            ResourceTable.Media_p20, //E
            ResourceTable.Media_p21, //F
            ResourceTable.Media_p23, //G
            ResourceTable.Media_p25, //A
            ResourceTable.Media_p27, //B
            //小字组
            ResourceTable.Media_p28,
            ResourceTable.Media_p30,
            ResourceTable.Media_p32,
            ResourceTable.Media_p33,
            ResourceTable.Media_p35,
            ResourceTable.Media_p37,
            ResourceTable.Media_p39,
            //小字一组
            ResourceTable.Media_p40,
            ResourceTable.Media_p42,
            ResourceTable.Media_p44,
            ResourceTable.Media_p45,
            ResourceTable.Media_p47,
            ResourceTable.Media_p49,
            ResourceTable.Media_p51,
            //小字二组
            ResourceTable.Media_p52,
            ResourceTable.Media_p54,
            ResourceTable.Media_p56,
            ResourceTable.Media_p57,
            ResourceTable.Media_p59,
            ResourceTable.Media_p61,
            ResourceTable.Media_p63,
            //大字组m
            ResourceTable.Media_p17,
            ResourceTable.Media_p19,
            ResourceTable.Media_p22,
            ResourceTable.Media_p24,
            ResourceTable.Media_p26,
            //小字组m
            ResourceTable.Media_p29,
            ResourceTable.Media_p31,
            ResourceTable.Media_p34,
            ResourceTable.Media_p36,
            ResourceTable.Media_p38,
            //小字一组m
            ResourceTable.Media_p41,
            ResourceTable.Media_p43,
            ResourceTable.Media_p46,
            ResourceTable.Media_p48,
            ResourceTable.Media_p50,
            //小字二组m
            ResourceTable.Media_p53,
            ResourceTable.Media_p55,
            ResourceTable.Media_p58,
            ResourceTable.Media_p60,
            ResourceTable.Media_p62
    };

    private SoundPlayer soundPlayer;
    private HashMap<Integer,Integer> soundPlayerMap;
    private SoundPlayer.AudioVolumes audioVolumes;
    private SoundPlayer.SoundPlayerParameters soundPlayerParameters;

    public PianoMusic(Context context) {
        soundPlayer = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
        soundPlayerParameters  = new SoundPlayer.SoundPlayerParameters();
        audioVolumes = new SoundPlayer.AudioVolumes();
        //设置音量 0-100f
        audioVolumes.setCentralVolume(100.0f);
        soundPlayerParameters.setVolumes(audioVolumes);
        //设置播放次数
        soundPlayerParameters.setLoop(SoundPlayer.SoundPlayerParameters.NO_LOOP);
        //设置播放速度
        soundPlayerParameters.setSpeed(1.0f);
        //设置播放优先级 PLAY_IMMEDIATELY_PRIORITY为自动销毁  PLAY_DELAYED_PRIORITY为延迟销毁
        soundPlayerParameters.setPriority(SoundPlayer.SoundPlayerParameters.PLAY_IMMEDIATELY_PRIORITY);
        soundPlayerMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < Music.length; i++) {
            //绑定音乐与按键序号
            soundPlayerMap.put(i,soundPlayer.createSound(context,Music[i]));
        }
    }

    public void soundPlay(int number) {
        //播放声音
        soundPlayer.play(soundPlayerMap.get(number),soundPlayerParameters);
    }
}
